<?php
/**
 * Created by PhpStorm.
 * User: N
 * Date: 2019/5/30
 * Time: 14:18
 */

namespace app\index\controller;


use app\index\model\doAdmin;
use app\index\model\doUser;
use think\Controller;
use think\facade\Request;
use think\Db;
use think\facade\Cookie;
class Login extends Controller
{
    // 登陆
    public function login()
    {
        // echo sha1("keti");die;
        if (Request::post()){
            
            $data = input('post.');

            if($data['name']=='root'){
                $where['name'] = $data['name'];
                $where['password'] = sha1($data['pass']);
                $admin = new doAdmin();
                $res = $admin::where($where)->find();
            }else{
               
                $where['idcard'] = $data['name'];
                $where['password'] = sha1($data['pass']);
                $admin = new doAdmin();
                $res = $admin::where($where)->find();
                
               
                if($res['lv']==2||$res['lv']==3){
                    $lv = $res['lv'];
                    $school = $res['school'];
              
                    $res1 = Db::name('cate')->where('id',$res['area'])->find();
                 
                    if($res1&&$lv==3){
                        $res2 = Db::name('cate')->where('id',$school)->find();
                        if(!$res2){
                            $res['status'] = 0;
                            return json($res);
                        }
                    }elseif(!$res1){
                         $res['status'] = 0;
                            return json($res);
                    }
                }
            }
                if ($res){
                   
                    // 登陆成功
                    Cookie('admin',json_encode($res));
                    $res['status'] = 1;
                    // echo 123;die;
                    return json($res);
                }
        
                $res['status'] = 0;
                $res['msg'] = '账号或密码错误';
                return json($res);

//            }
        }else{
            $code = input("code");
            $this->assign('code',$code);
            return $this->fetch();
        }
    }

    // 验证码判断
    public function checkCode($code){

        if(captcha_check($code)){
            return true;
        }
        else{
            return false;
        }
    }
    public function sendmessage(){
        $code = input("code");
        $phone = input("phone");
        $account = input('name');
        Db::name("admin")->where('phone',$phone)->where("idcard",$account)->find();
        if($code==sha1('renzheng')||$code==sha1('keti')){
            if(!Db::name("admin")->where('phone',$phone)->where("idcard",$account)->find()){
                ajaxMsg(0,'账号信息错误');
            }
        }elseif(!Db::name("admin")->where('phone',$phone)->where("idcard",$account)->find()){
            
            ajaxMsg(0,'账号信息错误');
        }
        
        $time = Cookie::get('time');
        if($time>time()-60){
            ajaxMsg(0,'验证码已发送');
        }
        $num = rand('1000','9999');
        $send_data['code'] = $num;

        Cookie::set('code',$num);
        Cookie::set('time',time());
        $res = sendMsg($phone,$send_data);
        ajaxMsg(1,'验证码已发送');
    }

    //忘记密码
    public function forget(){
        if(Request::isPost()){
            $data = input('post.');
            // dump($data);die;
            if($data['pass']!=$data['repass']){
                ajaxMsg(0,'两次密码不一致!');
            }
            $data['password'] = sha1($data['pass']);
            $phone = input('phone');
            
            $code1 = sha1("renzheng");$code2 = sha1("keti");

            $where['idcard'] = $data['name'];
         
            // dump($where);die;
            $res = Db::name("admin")->where($where)->update(['password'=>$data['password']]);
            if($res){
                ajaxMsg(1,'密码已修改,请重新登录');
            }else{
                ajaxMsg(0,'密码没有改变');
            }
            
        }
        $code = input("code");
        $this->assign('code',$code);
        return $this->fetch();
        
    }
}